import { Controller, Post, Body, UseGuards, Get, Param, Query, Session, HttpCode, HttpStatus, Res } from "@nestjs/common"
import { AuthGuard } from '@nestjs/passport';
import { Public } from "./decorators/public.decorator"
import { LoginService } from "./login.service"
@Controller('/v1/api/register')
export class LoginController {
    constructor(private readonly loginService: LoginService) {}

    @UseGuards(AuthGuard('jwt'))
    @Public()
    @HttpCode(HttpStatus.OK)
    @Post('/createUser')
    async addUser(@Session() session: Record<string, any>,  @Body() signInDto: Record<string, any>){
      // console.log('user', session, signInDto)
      const { captcha } = signInDto
      const sessionCaptcha = session.captcha || ''; // 在会话中的验证码
      if (!this.loginService.verifyCaptcha(captcha, sessionCaptcha)) {
        return {
          code: 400,
          data: null,
          message: '验证码输入错误',
        };
      }
      const res =  await this.loginService.createUser(signInDto.username, signInDto.password);
      return res;
    }
  
    @Public()
    @HttpCode(HttpStatus.OK)
    @Post('/login')
  async register(@Session() session: Record<string, any>,  @Body() signInDto: Record<string, any>, @Res({ passthrough: true }) res: any){
    // console.log('user', session, signInDto)
    const { captcha } = signInDto
    const sessionCaptcha = session.captcha || ''; // 在会话中的验证码
    // console.log('对比session', captcha, sessionCaptcha);
    // 验证用户输入的验证码是否正确
    if (!this.loginService.verifyCaptcha(captcha, sessionCaptcha)) {
      return {
        code: 400,
        data: null,
        message: '验证码输入错误',
      };
    }

    const result = await this.loginService.signIn(
      signInDto.username,
      signInDto.password,
    );
    console.log('result', result);
    if (res?.cookie && result?.data?.access_token) {
      res.cookie('token', result?.data?.access_token, {
        httpOnly: true ,
        // secure: true, // 仅在 HTTPS 下传输
        sameSite: 'strict',
      });
    }
    return result;
  }

  @Public()
  @HttpCode(HttpStatus.OK)
  @Post('/signup')
async signIn(@Session() session: Record<string, any>,  @Body() signInDto: Record<string, any>){
  // console.log('user', session, signInDto)
  const { captcha } = signInDto
  const sessionCaptcha = session.captcha || ''; // 在会话中的验证码
  // console.log('对比session', captcha, sessionCaptcha);
  // 验证用户输入的验证码是否正确
  if (!this.loginService.verifyCaptcha(captcha, sessionCaptcha)) {
    return {
      code: 400,
      data: null,
      message: '验证码输入错误',
    };
  }

  const result = await this.loginService.signup(
    signInDto.username,
    signInDto.password,
  );
  console.log('result', result);
  return result;
}

  @UseGuards(AuthGuard('jwt'))
  @Public()
  @Post('/logout')
  async logout(@Session() session: Record<string, any>, @Res({ passthrough: true }) res: any) {
    session.destroy();
    // res.clearCookie('token');
    res.cookie('token', '', { maxAge: 0 });
    return { message: 'Logout successful', code: 200 };
  }


  @Public()
  @Post('captcha')
  getCaptcha(@Session() session: Record<string, any>) {
    const captcha = this.loginService.generateCaptcha();
    session.captcha = captcha.text;  // 将验证码文本保存到会话中，用于后续验证
    console.log('session', session);
    if(captcha.text){
      return { code: 200, data: captcha, message: '验证码获取成功' };
    } else{
      return { code: 400, data: null, message: '验证码获取失败' };
    }
  }

 
}